package com.baseinfo.action;

import com.baseinfo.service.InstitutionsService;
import com.enums.ReturnCode;
import com.framework.base.controller.BaseViewController;
import com.framework.view.KxdParams;
import com.framework.view.ResultDatas;
import com.framework.view.UserInfoView;
import com.framework.view.ResultDatas;
import com.model.TInstitutions;
import com.model.TSysRole;
import com.utils.ReturnCodeUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

/**
 * @author admin
 * @date
 */
@Controller
@RequestMapping("/soa/baseinfo/institutions")
public class InstitutionsController extends BaseViewController {
    Logger logger = Logger.getLogger(InstitutionsController.class);

    @Resource(name = "institutionsServiceImpl")
    InstitutionsService service;

    /**
     * 跳转到首页
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "index", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public ModelAndView query (HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> params = new HashMap<String,Object>();
        UserInfoView<TSysRole> userInfo = getSessionUser(request);
        params.put("user", userInfo);
        return new ModelAndView("/baseinfo/Institutions/index", params);
    }

    /**
     * 分页查询所有的
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "datas/search", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object conditionsPaging (HttpServletRequest request, HttpServletResponse response, KxdParams params, Long start, Long length) throws IOException {
        ResultDatas resultDatas = new ResultDatas();
        List<TInstitutions> list = null;
        Long recordsTotal = null;
        Long recordsFiltered = null;

        if (null == params.getParams()) {
            recordsTotal = service.getTotalSize();
            //filter的数额是显示在DT的条目数
            recordsFiltered = recordsTotal;
            list = service.searchPage(start, length);
        } else {
            Map<String,Object> map = params.getParams();
            recordsTotal = service.getSearchSize(map);
            //filter的数额是显示在DT的条目数
            recordsFiltered = recordsTotal;
            list = service.conditionsPage(map, start, length);
        }
        if (list.size() > 0) {
            resultDatas.setSuccess(0);
            resultDatas.setMsg("拿到数据");
            resultDatas.setStart(start);
            resultDatas.setLength(length);
            resultDatas.setRecordsFiltered(recordsFiltered);
            resultDatas.setRecordsTotal(recordsTotal);
            resultDatas.setData(list);
        } else {
            resultDatas.setSuccess(1);
            resultDatas.setData("");
            resultDatas.setMsg("没有数据");
        }
        list = null;
        return resultDatas;

    }

    /**
     * 新增方法(主要做页面跳转到模态框取值)
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "inst", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView add (HttpServletRequest request, HttpServletResponse response) {

        return editModul(request, response, null);//撞到编辑页面
    }

    /**
     * 修改方法
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping(value = "edit", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView editModul (HttpServletRequest request, HttpServletResponse response, Long id) {
        TInstitutions model = null;
        if (id != null) {
            model = service.findById(id);
        } else {
            model = new TInstitutions();

        }

        return new ModelAndView("/baseinfo/Institutions/edit", "model", model);
    }


    /**
     * 保存对象，inst和edit最后都要走到这里
     *
     * @param request
     * @param response
     * @param modelParam
     * @return
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping(value = "datas/save", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object save (HttpServletRequest request, HttpServletResponse response, TInstitutions modelParam) throws IOException, ParseException {
        UserInfoView userInfo = this.getSessionUser(request);
        ResultDatas resultDatas = null;
        try {
            if (modelParam.getId() != null) {
                TInstitutions model = service.findById(modelParam.getId());
                model.setName(modelParam.getName());
                model.setType(modelParam.getType());
                model.setManager(modelParam.getManager());
                model.setDockingPerson(modelParam.getDockingPerson());
                model.setDockingPersonPhone(modelParam.getDockingPersonPhone());
                model.setMaterial(modelParam.getMaterial());
                model.setStatus(modelParam.getStatus());
                model.setIsStop(modelParam.getIsStop());
                service.saveOrupdate(model);
                resultDatas = new ResultDatas(0, "操作成功", model);
                modelParam = null;
                model = null;
                return resultDatas;
            } else {
                modelParam.setIsDeleted(0);
                modelParam.setIsStop(0);
                modelParam.setCreateTime(new Date());
                modelParam = service.save(modelParam);
                resultDatas = new ResultDatas(0, "操作成功", modelParam);
                modelParam = null;
                return resultDatas;
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultDatas = new ResultDatas(1, "操作失败", null);
            return resultDatas;
        }
    }


    @RequestMapping(value = "datas/del", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object del (HttpServletRequest request, HttpServletResponse response, Long id) throws IOException {
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            if (id != null) {
                service.del(id);
                result.put("msg", "删除成功");
                result.put("status", 0);
            } else {
                result.put("msg", "删除失败");
                result.put("status", 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg", "删除失败");
            result.put("status", 1);
        }
        return result;
    }


}
